import markdown
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse

from article.forms import ArticleForm
from article.models import Article
from comment.models import Comment


def index(request):
    all_article = Article.objects.all().order_by('-created_time')
    context = {
        'all_article': all_article
    }
    return render(request, 'index.html', context)


def article_detail(request, id):
    article = get_object_or_404(Article, pk=id)
    comments = Comment.objects.filter(article=id)
    # 将markdown语法渲染成html样式
    article.art_body = markdown.markdown(article.art_body,
                                         extensions=[
                                             # 包含 缩写、表格等常用扩展
                                             'markdown.extensions.extra',
                                             # 语法高亮扩展
                                             'markdown.extensions.codehilite',
                                         ])
    context = {
        'article': article,
        'comments': comments
    }
    return render(request, 'article_detail.html', context)


@login_required(login_url='bloguser:user_login')
def my_articles(request):
    my_articles = Article.objects.filter(author=request.user).order_by('-created_time')
    context = {
        'my_articles': my_articles,
    }
    for my_article in my_articles:
        my_article.art_body = markdown.markdown(my_article.art_body,
                                                extensions=[
                                                    # 包含 缩写、表格等常用扩展
                                                    'markdown.extensions.extra',
                                                    # 语法高亮扩展
                                                    'markdown.extensions.codehilite',
                                                ])
    return render(request, 'my_articles.html', context)


@login_required(login_url='bloguser:user_login')
def add_article(request):
    if request.method == 'POST':
        articleforms = ArticleForm(data=request.POST)
        if articleforms.is_valid():
            new_article = articleforms.save(commit=False)
            new_article.author = request.user
            new_article.save()
            return redirect(reverse('article:my_articles'))
        else:
            context = {
                'msg': '数据错误，添加失败'
            }
            return render(request, 'add_article.html', context)
    else:
        articleform = ArticleForm()
        context = {
            'articleform': articleform,
        }
        return render(request, 'add_article.html', context)


@login_required(login_url='bloguser:user_login')
def del_article(request, id):
    art_obj = get_object_or_404(Article, id=id)
    if art_obj.author == request.user:
        art_obj.delete()
        return redirect(reverse('article:my_articles'))
    else:
        context = {
            'msg': '删除失败'
        }
        return render(request, 'my_articles.html', context)


@login_required(login_url='bloguser:user_login')
def update_art(request, id):
    article = get_object_or_404(Article, id=id)
    if request.method == 'POST':
        article_update_form = ArticleForm(request.POST)
        if article_update_form.is_valid():
            data = article_update_form.cleaned_data
            article.art_title = data['art_title']
            article.art_body = data['art_body']
            article.save()
            # 修改完成，重定向到当前文章的详情
            return redirect(reverse('article:article_detail', kwargs={'id': id, }))
        else:
            article_update_form = ArticleForm()
            context = {
                'article': article,
                'article_update_form': article_update_form,
                'msg': '编辑失败',
            }
            return render(request, 'update_art.html', context)
    else:
        article_update_form = ArticleForm()
        context = {
            'article': article,
            'article_update_form': article_update_form,
        }
        return render(request, 'update_art.html', context)
